

local function upgrade_batbat_type1(inst)
    inst.CHAT_TABLE = {}
    pcall(function()
        inst.CHAT_TABLE = TUNING.NPC_CHAT_TABLE[ inst.npc_base_lib:Get_Language() ] ["batbat_upgrade_boss"] or {}
    end)

    
    inst.talk_sound = "hookline_2/characters/hermit/talk"
    inst:RemoveTag("shadowlevel")
    if inst.components.named.name == nil then
        inst.components.named.possiblenames = inst.npc_base_lib:Get_Names()
        inst.components.named:PickNewName()
    end

    local function Chat_Bubble_Fn(inst)
        local boss_name =  inst.npc_everything_data:Get("boss_name")
        if boss_name == nil or type(boss_name) ~= "string" then
            return
        end
        inst.npc_base_lib:Show_Chat_Bubble({
            monster = boss_name,            
            height = 2,
            sound_off = true,
        })
        inst.npc_base_lib:Say(inst.CHAT_TABLE.ask_for_target,true)
    end


    inst:ListenForEvent("ondropped",function()
        -- print("bat bat drop event")
        inst.__Chat_Bubble_Drop_Task =inst:DoTaskInTime(3,function()
            if inst.components.inventoryitem.owner == nil then
                Chat_Bubble_Fn(inst)
                inst:DoTaskInTime(5,function()
                    inst.npc_base_lib:Hide_Chat_Bubble()
                end)
            end
        end)
    end)

    inst:ListenForEvent("onpickup",function()
        if inst.__Chat_Bubble_Drop_Task then
            inst.__Chat_Bubble_Drop_Task:Cancel()
            inst.__Chat_Bubble_Drop_Task = nil
        end
        inst.npc_base_lib:Hide_Chat_Bubble()
        -- print("batbat pickup event")
    end)

    inst:DoPeriodicTask(10,function()
        if inst.components.inventoryitem.owner ~= nil then
            return
        end
        
        if inst:IsNearPlayer(20) == true then
            Chat_Bubble_Fn(inst)
            inst:DoTaskInTime(5,function()
                inst.npc_base_lib:Hide_Chat_Bubble()
            end)
        end

    end)

    ------------------------------------------------------------------------------------------------------------------------------------

    inst.components.weapon:SetDamage(1)
    inst.components.weapon.onattack = function(inst, owner, target)
        if owner == nil  or not owner:HasTag("player") then
            return
        end
        if target == nil or target.prefab == nil  or not target:IsValid() then
            return
        end

        if target.prefab ~= inst.npc_everything_data:Get("boss_name") then
            inst.npc_base_lib:Say(inst.CHAT_TABLE.worng_boss)
            if owner.components.health and owner.components.health.currenthealth > 5 then
                SpawnPrefab("npc_fx_bat_fly"):PushEvent("Set",{ pt = Vector3(owner.Transform:GetWorldPosition() )  })
                owner.components.health.currenthealth = 3
                owner.components.health:DoDelta(-1)
                if target.components.health then
                    target.components.health:Kill()
                end
            end


            return
        end

        if target.prefab == inst.npc_everything_data:Get("boss_name") then
            --- kill boss  and  owner . an announce
            if target.components.health then
                target.components.health:Kill()
            end
            if owner.components.health then
                -- owner.components.health:Kill()   --- kill 不能直接杀死旺达
                ----------------------------------------------------------------------------------
                local inst_name = inst.components.named.name or "Batbat"
                local str = "["..inst_name.."]"..inst.CHAT_TABLE.announce
                local announce =  string.gsub(str,"XXXX",owner:GetDisplayName(),1)
                ----------------------------------------------------------------------------------
                owner.npc_base_lib:Player_Killed_With_Announce(announce,"batbat_upgrade_boss")
                ---------------------------------------------------------------------------------
            end
            SpawnPrefab("npc_fx_bat_fly_tall"):PushEvent("Set",{ pt = Vector3(owner.Transform:GetWorldPosition() )  })
           
            inst:Remove()
        end    
    end

    if inst.npc_everything_data:Get("boss_name") == nil then
        local boss_name = inst.npc_base_lib:GetRandom({ "antlion","bearger","beequeen","crabking","deerclops","dragonfly","eyeofterror","klaus","malbatross","minotaur","toadstool","toadstool_dark","twinofterror1","twinofterror2" })
        inst.npc_everything_data:Set("boss_name",boss_name)
    end



    -- print("6666666666666666666666666666666666666")

end





AddPrefabPostInit(
    "batbat",
    function(inst)

        if inst.SoundEmitter == nil then
            inst.entity:AddSoundEmitter()
        end

        ---------------------------------------------------------------------------------------------
        -- talker 组件   net_bool 帮助处理偏移
        -- local TALLER_TALKER_OFFSET = Vector3(0, -700, 0)
        local colours = {
            Vector3( 255 /255, 193 /255 , 37 /255),     -- Goldenrod1 橙黄色
            Vector3( 127 /255, 255 /255 , 0 /255),      --- Chartreuse 青绿色
            Vector3( 15 /255, 15 /255 , 15 /255),      --- black 黑色
            Vector3( 255 /255, 69 /255 , 0 /255),       -- OrangeRed 橘红色
            Vector3( 0 /255, 191 /255 , 255 /255),      -- DeepSkyBlue 天蓝色
            Vector3( 34 /255, 139 /255 , 34 /255),      -- ForestGreen 深绿色
            Vector3( 238 /255, 233 /255 , 233 /255),     -- Snow2  雪白色
        }
        local DEFAULT_TALKER_OFFSET = Vector3(0, 0, 0)
        inst:AddComponent("talker")
        inst.components.talker.offset = DEFAULT_TALKER_OFFSET
        inst.components.talker.fontsize = 28
        inst.components.talker.font = TALKINGFONT
        inst.components.talker.color_num = 1
        inst.components.talker.colour = colours[inst.components.talker.color_num]
        inst.components.talker.offset = Vector3(0, 0, 0)
        inst.components.talker.Say_old_batbat = inst.components.talker.Say
        inst.components.talker.Say = function(self,...)
            self:Say_old_batbat(...)
            self.color_num = self.color_num + 1
            if self.color_num > #colours then
                self.color_num = 1
            end
            self.colour = colours[self.color_num]
        end
        ---------------------------------------------------------------------------------------------

        if not TheWorld.ismastersim then ---------------------------------------------------------------------------------------------
            return
        end

        -- inst:AddComponent("tradable")
        inst:AddComponent("npc_base_lib")
        inst:AddComponent("npc_everything_data")
    
        if inst.components.named == nil then
            inst:AddComponent("named")
        end
        ------------------------------------------------------------------------------------------------
        inst:ListenForEvent("upgrade",function(inst,str)
            if str == nil then
                return
            end
            if str == "boss" then
                upgrade_batbat_type1(inst)
                inst.components.npc_everything_data:Set("upgrade","boss")
            end
        end)

        inst:DoTaskInTime(0,function()
            local the_type =  inst.components.npc_everything_data:Get("upgrade")
            if the_type == "boss" then
                upgrade_batbat_type1(inst)
            end
        end)
        ------------------------------------------------------------------------------------------------
    end
)